import { ArgumentMetadata, HttpException, HttpStatus, Injectable, PipeTransform } from '@nestjs/common';
import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';
@Injectable()
export class RoleConfigAuthPipe implements PipeTransform {
  async transform(value: any, metadata: ArgumentMetadata) {
    const dto = plainToInstance(metadata.metatype, value);
    const errors = await validate(dto);
    if (errors.length > 0) {
        throw new HttpException(errors, HttpStatus.BAD_REQUEST);
    }
    if (dto.menus.indexOf(dto.menuId) < 0 && dto.actions.length > 0) {
        throw new HttpException('menuId参数错误, 没有选择菜单项', HttpStatus.BAD_REQUEST);
    }
    return value;
  }
}
